package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;

import cn.wolfcode.car.business.service.IStatementItemServicePlus;
import cn.wolfcode.car.business.service.IStatementServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.Date;

/**
 * 服务单项控制器
 */
@Controller
@RequestMapping("business/statement")
public class StatementController {
    //模板前缀
    private static final String prefix = "business/statement/";

    @Autowired
    private IStatementServicePlus statementServicePlus;
    @Autowired
    private IStatementItemServicePlus statementItemServicePlus;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:statement:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:statement:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("business:statement:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("statement", statementServicePlus.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:statement:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Statement> query(StatementQuery qo){
        return statementServicePlus.query(qo);
    }


    //新增
    @RequiresPermissions("business:statement:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Statement statement){
        //由于页面中没有给创建时间和状态添加文本框，因此需要手动设置或给默认值
        /*statement.setCreateTime(new Date());
        //默认状态为消费中
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);*/

        UpdateWrapper<Statement> wrapper = new UpdateWrapper<>();
        wrapper.set("create_time",new Date())
                .set("status",Statement.STATUS_CONSUME);
        statementServicePlus.save(statement);
        return AjaxResult.success();
    }


    //编辑
    @RequiresPermissions("business:statement:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Statement statement){
        //只要是消费中的状态才可以进行编辑
        /*Statement oldObj = this.get(statement.getId());
        if(!Statement.STATUS_CONSUME.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于消费中的状态才可以进行编辑");
        }
        */
        Statement oldObj = statementServicePlus.getById(statement.getId());
        if(!Statement.STATUS_CONSUME.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于消费中的状态才可以进行编辑");
        }
        UpdateWrapper<Statement> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",statement.getId());
        statementServicePlus.update(statement,wrapper);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:statement:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        statementServicePlus.removeById(ids);
        //在删除结算单数据时也需要把关联的结算单明细一并删除
        QueryWrapper<StatementItem> wrapper = new QueryWrapper<>();
        wrapper.eq("statement_id", ids);
        statementItemServicePlus.remove(wrapper);
        return AjaxResult.success();
    }



    @RequestMapping("/exportOP")
    public void exportOP(Long statementId,HttpServletResponse response) throws IOException {
        //设置响应头信息
        response.setHeader("Content-Disposition","attachment;filename=statementData.xlsx");
        statementServicePlus.exportOP(statementId,response);
    }


}
